package com.yy.common.commonutils;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.text.DecimalFormat;

/**
 *
 * Created by Ellis.Li on 2017/6/14.
 */
public class StringUtil {
    /**
     * @brief 把byte[]数组转换成十六进制字符串表示形式
     * @param tmp    要转换的byte[]
     * @return 十六进制字符串表示形式
     */
    private static String byteToHexString(byte[] tmp) {
        String s;
        final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f' };// 用来将字节转换成16进制表示的字符
        // 用字节表示就是 16 个字节
        char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话，使用两个字符，
        // 所以表示成 16 进制需要 32 个字符
        int k = 0; // 表示转换结果中对应的字符位置
        for (int i = 0; i < 16; i++) { // 从第一个字节开始，对 MD5 的每一个字节
            // 转换成 16 进制字符的转换
            byte byte0 = tmp[i]; // 取第 i 个字节
            str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
            // >>> 为逻辑右移，将符号位一起右移
            str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
        }
        s = new String(str); // 换后的结果转换为字符串
        return s;
    }
    public static boolean isEmpty(String text) {
        if (text == null || text.trim().equals(""))
            return true;
        return false;
    }


    public  static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        int height =     totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        Log.d("StringUtil","height :"+height);
        params.height = height;
        listView.setLayoutParams(params);
    }

    public  static void setGridViewHeightBasedOnChildren(GridView gridView,int count,int numColumns) {
        ListAdapter listAdapter = gridView.getAdapter();
        int size = (int)Math.ceil((double) count/(double)numColumns);
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < size; i++) {
            View listItem = listAdapter.getView(i, null, gridView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        int height =     totalHeight;
        Log.d("StringUtil","height :"+height);
        params.height = height;
        gridView.setLayoutParams(params);
    }

    /**
     * 四舍五入
     * @param content
     * @return
     */
    public static String getMathRound(double content){
        long round = Math.round(content);
        return String.valueOf(round);
    }

    /**
     * 格式化文件大小
     * @param fileSize
     * @return
     */
    public static String formatFileSize(String fileSize){
         return getMathRound(Double.parseDouble(fileSize)/1024)+"Mb";
    }

    /**
     * 字符串添加空格
     * @param para
     * @return
     */
    public static String appendSpace(String  para){
        if (StringUtil.isEmpty(para))return "";
        int length = para.length();
        char[] value = new char[length << 1];
        for (int i=0, j=0; i<length; ++i, j = i << 1) {
            value[j] = para.charAt(i);
            value[1 + j] = ' ';
        }
        return new String(value);
    }

    /**
     * double保留两位小数点
     * @param para
     * @return
     */
    public static String getSingleDouble(double  para){
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        return decimalFormat.format(para);
    }

    /**
     * double保留两位小数点
     * @param para
     * @return
     */
    public static String getSingleDouble(String  para){
        if (StringUtil.isEmpty(para))return "";
        try {
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            return decimalFormat.format(Double.parseDouble(para));
        }catch (Exception e){

        }
        return "";
    }

    /**
     * @param para
     * @return
     */
    public static String getDeleteDecimal (double  para){
        try {
            DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            return decimalFormat.format(para);
        }catch (Exception e){

        }
        return "";
    }

    /**
     * 省略中间字符串
     * @param phone
     * @return
     */
    public static String getPhoneMiddle (String phone){
        try {
            if (isEmpty(phone))return "";
                return  phone.substring(0,3)+"****"+ phone.substring(7);
        }catch (Exception e){

        }
        return "";
    }

}
